<template>
  <!--  修改申请订单-->
  <div class="marketPushDown" @click.stop="aotuTab=false">
    <div style="display: flex">
      <jc-button
        :btn-arr="['pass', 'sub', 'remove', 'save', 'hangPass', 'termin', 'terentry', 'push', 'option', 'reload', 'list', !list.length && 'check']"
        :disab-arr="['using', 'cancel', 'toExcel', 'freeze']"
        :fbillno="optionValue.fbillno"
        :list="'/applyList'"
        @refresh="getForm"
        @save="save"
        @submit="submit(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="delApply"
        @forbidden="using(1)"
        @startUsing="using(2)"
        @termin="termin(1)"
        @termin1="termin(2)"
        @terentry="terentry(1)"
        @terentry1="terentry(2)"
        @hangAudit="hangAudit(1)"
        @hangAudit2="hangAudit(2)"
        @pushDown="isdanjuDialog = true"
        @check="check('up')"
        @check1="check('down')"
        @handleCom1="queryReportForm1(1)"
        @handleCom2="queryReportForm1(2)"
      />
      <el-button style="margin-left: 10px;" size="mini" type="primary" @click="pushView">{{ $t('market.backApprove') }}</el-button>
    </div>
    <el-tabs v-model="activeName" class="tabs" @tab-click="handleOther">
      <el-tab-pane :label="$t('market.info')" name="basic">
        <el-form
          ref="jcForm"
          size="mini"
          label-width="80px"
          :model="optionValue"
        >
          <el-form-item :label="$t('market.state')">
            <el-tag :type="optionValue.stateColor">{{ optionValue.fdocumentStatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.billType')" prop="fbilltype">
            <el-input v-model="optionValue.fbilltype" disabled />
          </el-form-item>
          <!--          <el-form-item label="采购组织">-->
          <!--            <el-input v-model.trim="optionValue.fpurchaseorg" disabled />-->
          <!--          </el-form-item>-->
          <el-form-item :label="$t('market.billNo')">
            <el-input v-model="optionValue.fbillno" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.applicant')" prop="fapplicant">
            <el-autocomplete
              v-model.trim="optionValue.fapplicant"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch19"
              size="mini"
              :disabled="isDisabled"
              @focus="handleApplicant"
              @select="selectApply"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="userVisiblit = true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item :label="$t('market.applyDate')">
            <el-date-picker
              v-model="optionValue.fdate"
              value-format="yyyy-MM-dd"
              size="mini"
              type="date"
              :disabled="isDisabled"
              class="input-width"
            />
          </el-form-item>
          <el-form-item :label="$t('market.usedepid')">
            <el-autocomplete
              v-model.trim="optionValue.fdeptName"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch8"
              :disabled="isDisabled"
              @focus="getBasicUnit('SX_V_WORKSHOP')"
              @select="selUnit"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="unitVisabled=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item :label="$t('market.note')" prop="fnote" style="width: 33.3%;">
            <el-input v-model="optionValue.fnote" :disabled="isDisabled" style="width: 100%" />
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.fince')" name="first">
        <el-form
          ref="jcForm"
          :model="optionValue"
          size="mini"
          label-width="80px"
        >
          <!--          <el-form-item label="结算组织">-->
          <!--            <el-input v-model.trim="optionValue.fsettleorg" disabled />-->
          <!--          </el-form-item>-->
          <el-form-item :label="$t('market.currency2')" prop="fsettleCurrId">
            <el-autocomplete
              v-model.trim="optionValue.fsettleCurr"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch5"
              size="mini"
              :disabled="isDisabled"
              @focus="queryTBdCurrency"
              @select="currencyData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="currencyVisiblit=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item :label="$t('market.exchangeType')" prop="fexchangetypeid">
            <el-autocomplete
              v-model.trim="optionValue.fexchangetype"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch20"
              size="mini"
              :disabled="isDisabled"
              @focus="queryRateType"
              @select="ratesData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="rateTypeVisiblit=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item :label="$t('market.exchangeRate')">
            <el-input v-model="optionValue.fexchangeRate" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.localCurr')">
            <el-input v-model="optionValue.flocalCurr" disabled />
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="optionValue.fbillno"
          other-height="68vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <div v-if="activeName!=='log'" style="margin-bottom: 5px;margin-top: 10px;">
      <el-button size="mini" :disabled="isDisabled" @click="newRow">
        {{ $t('btn.newHang') }}
      </el-button>
      <el-button size="mini" @click="delHang">{{ $t('btn.delHang') }}<!--删除行--></el-button>
      <el-button size="mini" @click="batchFill">{{ $t('btn.batchFill') }}<!--批量填充--></el-button>
    </div>
    <div v-if="activeName!=='log'">
      <jc-table
        ref="table"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        table-selection
        :get-summaries="getSummaries"
        show-summary
        serial
        @cellClick="cellClick"
        @selectionChange="selectionData"
      >
        <el-table-column
          :label="$t('market.fmaterial')"
          prop="fnumber"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-input
              v-model.trim="scope.row.fnumber"
              size="mini"
              :disabled="scope.row.fstop==='B' || scope.row.fdocumentstatus === 'B'"
              @input="getAtuo(scope.$index, scope.row.fnumber)"
            >
              <i
                slot="prefix"
                class="iconfont icon-jin-rud-ao-bo"
                @click="sonJumpMateriel(scope.row.fnumber)"
              />
              <i
                v-if="!isDisabled"
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="handleMateriel(scope.$index)"
              />
            </el-input>
            <div v-if="aotuTab && materielIndex === scope.$index" :class="materielIndex<7 ? 'tabPosit': 'tabPosit1'">
              <number-code :fnumber="fnumber" @material="materialData" />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('shop.fname2')"
          prop="fname"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('shop.specifica')"
          prop="fspecificaTion"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('market.fmodel')"
          prop="fmodel"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('market.funit')"
          prop="funit"
          align="center"
          width="80"
        />
        <el-table-column
          :label="$t('market.appQty')"
          prop="freqqty"
          align="center"
          width="80"
        >
          <template slot-scope="scope">
            <el-input-number
              v-model.number="scope.row.freqqty"
              :disabled="scope.row.fstop==='B' || scope.row.fdocumentstatus === 'B'"
              :min="0"
              size="mini"
              @change="fqeyhendel(scope.$index)"
            />
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('market.approveqty')"
          prop="fapproveqty"
          align="center"
          width="80"
        />
        <el-table-column
          :label="$t('market.yipushqty')"
          prop="fpushqty"
          align="center"
          width="80"
        />
        <el-table-column
          :label="$t('market.branche')"
          prop="branches"
          align="center"
          width="80"
        />
        <el-table-column
          :label="$t('market.stockQty')"
          prop="fbaseqty"
          align="center"
          width="80"
        />
        <el-table-column
          :label="$t('market.stockUnit')"
          prop="fstockunit"
          align="center"
          width="80"
        />
        <el-table-column :label="$t('market.stock2')" prop="fstock" align="center" width="100">
          <template slot-scope="scope">
            <el-autocomplete
              v-model.trim="scope.row.fstock"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch21"
              size="mini"
              :disabled="scope.row.fstop==='B' || scope.row.fdocumentstatus === 'B'"
              @focus="handleWarehouse(scope.$index)"
              @select="selectData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="handleCK(scope.$index)"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </template>
        </el-table-column>
        <el-table-column :label="$t('market.paezText1')" prop="fpaezText" align="center" width="120">
          <template slot-scope="scope">
            <el-input
              v-model.trim="scope.row.fpaezText"
              size="mini"
              :disabled="scope.row.fstop==='B' || scope.row.fdocumentstatus === 'B'"
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('market.productmodel')" prop="fproductmodel" align="center">
          <template slot-scope="scope">
            <el-input
              v-model.trim="scope.row.fproductmodel"
              size="mini"
              :disabled="scope.row.fstop==='B' || scope.row.fdocumentstatus === 'B'"
            />
          </template>
        </el-table-column>
        <!--        <el-table-column label="库存组织" align="center">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-select v-model="scope.row.fstockorgid" :disabled="isDisabled">-->
        <!--              <el-option-->
        <!--                v-for="option in teamList"-->
        <!--                :key="option.fid"-->
        <!--                :label="option.fname"-->
        <!--                :value="option.fid"-->
        <!--              />-->
        <!--            </el-select>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column
          :label="$t('market.chargeunit1')"
          prop="fbaseunit"
          align="center"
          width="80"
        />
        <el-table-column
          :label="$t('market.chargeQty1')"
          prop="fbaseunitqty"
          align="center"
          width="80"
        >
          <template slot-scope="scope">
            <el-input-number
              v-model.number="scope.row.fbaseunitqty"
              :disabled="scope.row.fstop==='B' || scope.row.fdocumentstatus === 'B'"
              :min="0"
              size="mini"
              @change="baseunitChange(scope.$index)"
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('market.note')" prop="fentrynote" align="center" width="120">
          <template slot-scope="scope">
            <el-input
              v-model.trim="scope.row.fentrynote"
              :disabled="scope.row.fstop==='B' || scope.row.fdocumentstatus === 'B'"
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('market.businessEnd1')" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.fstop==='A'">{{ $t('market.normal') }}</el-tag>
            <el-tag v-else-if="scope.row.fstop==='B'" type="danger">{{ $t('btn.termin') }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('market.hangStates')" prop="cbm" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.fdocumentstatus === 'B'">{{ $t('market.state2') }}</el-tag>
            <el-tag v-else type="danger">{{ $t('market.state6') }}</el-tag>
          </template>
        </el-table-column>
      </jc-table>
    </div>
    <!--币别-->
    <currency v-if="currencyVisiblit" @currency="currencyData" />
    <!--汇率类型-->
    <ratesType v-if="rateTypeVisiblit" @rates="ratesData" />
    <!--    物料弹窗-->
    <material v-if="isMateria" @material="materialData" />
    <!--申请人-->
    <user v-if="userVisiblit" @supplier="selectApply" />
    <!--  数据字典弹窗  -->
    <unit v-if="unitVisabled" msg="SX_V_WORKSHOP" @unit="selUnit" />
    <!-- 仓库 -->
    <selectCK
      v-if="seleceVisiblit"
      @client="selectData"
    />
    <!--  下推单据  -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.checkBill')"
      model
      top="5vh"
      :visible.sync="isdanjuDialog"
      :close-on-click-modal="false"
      width="600px"
      center
    >
      <div style="text-align: center;height: 100px;line-height: 100px">
        <el-radio-group v-model="orderLX">
          <el-radio v-for="item in receipts" :key="item" :label="item">
            {{ item }}
          </el-radio>
        </el-radio-group>
      </div>
      <hr>
      <div style="text-align: center;">
        {{ $t('market.rule') }}
        <el-input v-model="convert" size="mini" disabled style="width: 178px;margin-bottom: 5px;" />
        <br>
        {{ $t('market.billType') }}
        <el-select
          v-model.trim="fbillTypeId"
          size="mini"
          filterable
        >
          <el-option
            v-for="(item, index) in billTypeData"
            :key="index"
            :label="item.fname"
            :value="item.fid"
          />
        </el-select>
      </div>
      <div style="text-align: center;height: 50px;padding: 20px 0;">
        整单合并:
        <el-checkbox v-model="fmerge" style="line-height: 28px">全部分录行一并下推</el-checkbox><br>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isdanjuDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button size="mini" type="primary" @click="pushDownOrder()">
          {{ $t('btn.confirm') }}
        </el-button>
      </span>
    </el-dialog>
    <upDown v-if="upDownVis" :msg="cha" :fentryid="fentryData1" @client="upDownVis=false" />
  </div>
</template>
<script>
import {
  queryRequistionDetails, insertPoorder, basicprocurement, procurementbasic
} from '@/api/purchaseManagement/apply'
import { updateRequistion } from '@/api/purchaseManagement/apply'
import { notoMxAmina, toMxAmina } from '@/components/ToMxamineState'
import jcMarker from '@/components/marker'
import jcButton from '@/components/Button'
import ratesType from '@/views/basic/currencyRates/rates/components/rateType'
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import {
  audit,
  deleteRequistion, deletes, reAudit, requistionAudit, requistionAuStop,
  requistionAuSubmit, requistionClose, requistionDtlAuStop, requistionDtlStop, requistionStop,
  requistionSubmit,
  requistionUnAudit, requistionUnClose, update
} from '@/api/purchaseManagement/purchaseOrder'
import user from '@/views/basic/users/userslist/component/user'
import { queryRate, queryViewItem } from '@/api/marketManage/marketOrder'
import bility from '@/mixin/Operability'
import jumpMateriel from '@/components/JumpMateriel'

export default {
  name: 'EditApply',
  components: {
    unit: () => import('@/views/basic/createMateriel2/components/unit'),
    upDown: () => import('@/components/upDown'),
    currency: () => import('@/views/market/marketManage/createMarkerOrder/components/currency'),
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material'),
    jcMarker,
    selectCK: () => import('@/views/production/collect/components/seleceCK'),
    jcButton,
    ratesType,
    user,
    numberCode: () => import('@/components/NumberCode')
  },
  mixins: [jumpMateriel, autocomplete, bility],
  props: {
    list: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      unitVisabled: false,
      upDownVis: false,
      fmerge: false,
      cha: '',
      fentryData1: [],
      activeName: 'basic', // 基本信息
      optionValue: {},
      tableData: [],
      fentryData: [],
      tableHeader: [],
      tableHeight: '68vh',
      isdanjuDialog: false,
      orderLX: this.$t('menu.purOrderAsi'),
      receipts: [this.$t('menu.purOrderAsi')], // 生产'采购订单'
      fbillTypeId: 159,
      convert: this.$t('market.cgsqzcgdd'),
      billTypeData: [], // 采购订单单据
      isMateria: false, // 物料弹窗
      currencyVisiblit: false, // 币别
      rateTypeVisiblit: false, // 汇率类型
      seleceVisiblit: false, // 仓库
      userVisiblit: false, // 仓库
      materielIndex: 0, // 物料
      isDisabled: false, // 不可用
      otherUrlObject: {},
      aotuTab: false,
      fnumber: ''
    }
  },
  created() {
    this.getForm()
    this.purpoorder()
  },
  methods: {
    // 输入物料编码
    getAtuo(index, fnumber) {
      this.materielIndex = index
      this.aotuTab = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber = fnumber
      }, 600)
    },
    // 保存
    save(ev) {
      const parameter = {
        ...notoMxAmina(this.optionValue),
        details: this.tableData
      }
      const api = this.isDisabled ? update : updateRequistion
      api(parameter).then(res => {
        const { code, message, data } = res
        if (code !== 0) {
          this.$message.warning(message)
          return
        }
        ev === 1 ? this.submit(1) : this.getForm()
        this.$message.success(`${message}${data}`)
      })
    },
    // 物料编码
    handleMateriel(index) {
      this.materielIndex = index
      this.isMateria = true
    },
    // 获取物料
    materialData(item) {
      const {
        fnumber,
        fpurchaseUnit,
        fpurchaseUnitId,
        fspecificaTion,
        fmodel,
        fname,
        fdescripTion,
        fmaterialId,
        fstock,
        fstockId,
        funitId,
        fpurchasepriceUnit,
        fpurchasepriceUnitId
      } = item
      const tableData = this.tableData[this.materielIndex]
      tableData.fnumber = fnumber
      tableData.funit = fpurchaseUnit
      tableData.funitId = fpurchaseUnitId
      tableData.fbaseunitid = funitId
      tableData.fspecificaTion = fspecificaTion
      tableData.fmodel = fmodel
      tableData.fname = fname
      tableData.fstock = fstock
      tableData.fstockId = fstockId
      tableData.fpriceunit = fpurchasepriceUnit
      tableData.fpriceunitid = fpurchasepriceUnitId
      tableData.fdescripTion = fdescripTion
      tableData.fmaterialId = fmaterialId // 用来保证用户是否选择了物料编码(临时参数)
      this.isMateria = false
    },
    // 批准数量
    fqeyhendel(index) {
      const { fbaseunitid, freqqty, fbaseunitqty, fmaterialId, fnumber, funitId } = this.tableData[index]
      const DATA = {
        fbaseunitid, fbaseunitqty, fmaterialId, fnumber, funitId, freqqty
      }
      procurementbasic(DATA).then(res => {
        if (res.code === 0) {
          const item = this.tableData[index]
          item.fapproveqty = item.freqqty
          item.fbaseunitqty = res.data.baseunitqty
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 监听基本数量
    baseunitChange(index) {
      const { fbaseunitid, freqqty, fbaseunitqty, fmaterialId, fnumber, funitId } = this.tableData[index]
      const DATA = {
        fbaseunitid, fbaseunitqty, fmaterialId, fnumber, funitId, freqqty
      }
      basicprocurement(DATA).then(res => {
        if (res.code === 0) {
          const item = this.tableData[index]
          item.freqqty = res.data.priceunitqty
          item.fapproveqty = res.data.priceunitqty
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 币别
    currencyData(item) {
      if (item.fsettleCurrId || item.fcurrencyId) {
        this.optionValue.fsettleCurr = item.fsettleCurrIdName || item.fname
        this.optionValue.fsettleCurrId = item.fsettleCurrId || item.fcurrencyId
        this.querySalerRate()
      }
      this.currencyVisiblit = false
    },
    // 查询销售订单汇率
    async querySalerRate() {
      const DATA = {
        tocurrId: this.optionValue.flocalCurrId, // 本位币
        fromcurrId: this.optionValue.fsettleCurrId, // 结算币别
        queryDate: this.optionValue.fdate
      }
      const { data: RES, code, message } = await queryRate(DATA)
      if (code === 0) {
        this.optionValue.fexchangeRate = RES.exchangeRate
        this.optionValue.fexchangeType = RES.exchangeTypeName
        this.optionValue.fexchangeTypeId = RES.exchangeTypeId
      } else {
        this.optionValue.fexchangeRate = ''
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 仓库
    handleCK(index) {
      this.materielIndex = index
      this.seleceVisiblit = true
    },
    pushView() {
      this.$router.push('/approveList')
    },
    // 获取仓库数据
    selectData(ev) {
      if (ev.fstockId) {
        this.tableData[this.materielIndex].fstockId = ev.fstockId
        this.tableData[this.materielIndex].fstock = ev.fname
      }
      this.seleceVisiblit = false
    },
    // 汇率类型
    ratesData(ev) {
      if (ev.fid) {
        this.optionValue.fexchangetypeid = ev.fgroupId
        this.optionValue.fexchangetype = ev.fgroupName
      }
      this.rateTypeVisiblit = false
    },
    // 申请人双击
    selectApply(item) {
      if (item.fuserId) {
        this.optionValue.fapplicant = item.fname
        this.optionValue.fapplicantId = item.fuserId
      }
      this.userVisiblit = false
    },
    // 数据字典选中
    selUnit(ev) {
      if (ev.fid) { // 生产部门
        this.optionValue.fusedept = ev.fid
        this.optionValue.fdeptName = ev.fname
      }
      this.unitVisabled = false
    },
    // 多选
    selectionData(val) {
      this.delfnumber = val[0]?.fnumber
      this.fentryData = []
      this.fidsData = []
      val.forEach(res => {
        this.fentryData.push(res.fentryId)
        this.fidsData.push(res.fid)
      })
      this.fidsData = Array.from(new Set(this.fidsData))
    },
    // 下推采购订单
    pushDownOrder() {
      this.fentryData = []
      this.tableData.forEach(res => {
        this.fentryData.push(res.fentryId)
      })
      if (this.orderLX === this.$t('menu.purOrderAsi')) {
        const DATA = {
          SoucrceIds: [this.optionValue.fid],
          EntryIds: this.fentryData,
          fbillTypeId: this.fbillTypeId,
          fmerge: this.fmerge
        }
        insertPoorder(DATA).then(res => {
          if (res.code === 0) {
            this.$router.push({
              path: '/createPurchase',
              query: {
                id: DATA
              }
            })
            this.isdanjuDialog = false
          } else {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
      }
    },
    // 采购订单单据
    async purpoorder() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_PUR_POORDER' })
      this.billTypeData = RES.list
    },
    // 新增行
    newRow() {
      const DATA = {
        fentrynote: '',
        fnumber: '',
        funit: '',
        fname: '',
        fspecificaTion: '',
        fmodel: '',
        freqqty: 0,
        fapproveqty: 0,
        fsupplier: '',
        fstock: '',
        farrivalDate: new Date()
      }
      this.tableData.push(DATA)
    },
    // 删除行
    delHang() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableData.forEach((res, index) => {
          if (res.fnumber === this.delfnumber && res.fstop !== 'B') {
            if (res.fentryId) {
              const api = deletes
              this.toExamineFun(api, 2)
              this.tableData.splice(index, 1)
              this.$refs.table.clearSelect()
            } else {
              this.tableData.splice(index, 1)
              this.$refs.table.clearSelect()
            }
          }
        })
      })
    },
    // 点击单元格
    cellClick(row, column, cell, event) {
      this.property = column.property
      this.subscript = row.index
    },
    // 批量填充
    batchFill() {
      const item = this.tableData[this.subscript][this.property]
      console.log(item)
      const fstock = this.tableData[this.subscript].fstockId
      this.tableData.map((res, index) => {
        if (index >= this.subscript) {
          if (this.property === 'freqqty') {
            res.fapproveqty = item
          } else if (this.property === 'fstock') {
            res.fstockId = fstock
          }
          res[this.property] = item
        }
      })
    },
    // 获取数据
    async getForm() {
      const id = this.$route.params.id
      const { data: detailData, code, message } = await queryRequistionDetails({
        fid: id
      })
      if (code !== 0) {
        this.$message.warning(message)
        return
      }
      this.optionValue = detailData
      const { fdocumentStatus } = toMxAmina(detailData)
      this.otherUrlObject = detailData
      this.tableData = detailData.details
      if (fdocumentStatus === this.$t('market.state2') || fdocumentStatus === this.$t('market.state1') || fdocumentStatus === this.$t('market.state7')) {
        this.isDisabled = true
      } else {
        this.isDisabled = false
      }
    },
    // 公共函数
    toExamineFun(api, ev) {
      const DATA = {
        Numbers: [this.optionValue.fbillno],
        EntryIds: ev === 2 ? this.fentryData : [],
        fIds: ev === 2 ? [this.optionValue.fid] : []
      }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.getForm()
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? requistionSubmit : requistionAuSubmit
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? requistionAudit : requistionUnAudit
      this.toExamineFun(api)
    },
    // 关闭
    using(ev) {
      const api = ev === 1 ? requistionClose : requistionUnClose
      this.toExamineFun(api)
    },
    // 终止
    termin(ev) {
      const api = ev === 1 ? requistionStop : requistionAuStop
      this.toExamineFun(api)
    },
    // 明细行终止
    terentry(ev) {
      const api = ev === 1 ? requistionDtlStop : requistionDtlAuStop
      this.toExamineFun(api, 2)
    },
    // 明细行审核
    hangAudit(ev) {
      const api = ev === 1 ? audit : reAudit
      this.toExamineFun(api, 2)
    },
    // 删除
    delApply() {
      if (this.isDisabled) {
        this.$confirm('请先反审核后再删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        return
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const DATA = { Numbers: [this.optionValue.fbillno] }
          deleteRequistion(DATA).then(res => {
            if (res.code !== 0) {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
              return
            }
            // 关闭当前页面
            this.$store.dispatch('tagsView/guanbi', this.$route)
            // 跳转到修改页面
            this.$router.push({ path: '/applyList' })
          })
        })
    },
    // 上查、下查
    check(ev) {
      this.fentryData1 = []
      this.tableData.forEach(res => {
        this.fentryData1.push(res.fentryId)
      })
      this.cha = ev
      this.upDownVis = true
    },
    // 套打 连打
    queryReportForm1(ev) {
      const FNUMBER = this.optionValue.fbillno
      if (ev === 1) {
        this.$router.push({ path: `/applyTong1/${FNUMBER}` })
      } else {
        this.$router.push({ path: `/applyLian1/${FNUMBER}` })
      }
    },
    handleOther(tab) {
      if (tab.name === 'log') {
        this.$refs.log.otherlog()
      }
    }
  }
}
</script>
<style scoped lang="scss">
.marketPushDown {
  @include listBom;
  padding: 10px;
  background: #FFFFFF;
  .tabs {
    height: 100px;
  }
  .tabPosit {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: 3.5vh;
    left: 0;
    background-color: rgba(255, 255, 255, 1);
    z-index: 99999!important;
  }
  .tabPosit1 {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: -25.69vh;
    left:0;
    background-color: rgba(255, 255, 255, 1);
    z-index: 9999;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 35px;
      width: 275px;
      min-width: 246px;
      margin-bottom: 5px;

      .el-date-editor.el-input {
        width: 100% !important;
      }

      .el-select {
        width: 100%;
      }
    }
  }
}
</style>
